#coding=utf-8
#Boa:Dialog:DlgLog

import wx

from product import CDataLog
from ctrlAider import *

def create(parent):
    return DlgLog(parent)

[wxID_DLGLOG, wxID_DLGLOGBTNDEL, wxID_DLGLOGBTNEXPORT, wxID_DLGLOGBTNREFRESH, 
 wxID_DLGLOGLCLOG, 
] = [wx.NewId() for _init_ctrls in range(5)]

class DlgLog(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGLOG, name=u'DlgLog', parent=prnt,
              pos=wx.Point(347, 120), size=wx.Size(933, 650),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u7cfb\u7edf\u65e5\u5fd7')
        self.SetClientSize(wx.Size(925, 616))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.lcLog = wx.ListCtrl(id=wxID_DLGLOGLCLOG, name=u'lcLog',
              parent=self, pos=wx.Point(8, 48), size=wx.Size(904, 560),
              style=wx.LC_HRULES | wx.LC_REPORT | wx.LC_VRULES)

        self.btnDel = wx.Button(id=wxID_DLGLOGBTNDEL,
              label=u'\u5220\u9664\u9009\u4e2d\u9879', name=u'btnDel',
              parent=self, pos=wx.Point(144, 16), size=wx.Size(96, 26),
              style=0)
        self.btnDel.Enable(False)

        self.btnRefresh = wx.Button(id=wxID_DLGLOGBTNREFRESH,
              label=u'\u5237\u65b0', name=u'btnRefresh', parent=self,
              pos=wx.Point(8, 16), size=wx.Size(93, 26), style=0)
        self.btnRefresh.Bind(wx.EVT_BUTTON, self.OnBtnRefreshButton,
              id=wxID_DLGLOGBTNREFRESH)

        self.btnExport = wx.Button(id=wxID_DLGLOGBTNEXPORT,
              label=u'\u5bfc\u51fa\u5230Excel', name=u'btnExport', parent=self,
              pos=wx.Point(264, 16), size=wx.Size(112, 26), style=0)
        self.btnExport.Bind(wx.EVT_BUTTON, self.OnBtnExportButton,
              id=wxID_DLGLOGBTNEXPORT)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()

    def myInit(self):
        cls = CDataLog
        cols = [(cls.fi_id, wx.LIST_FORMAT_LEFT, 0),
                (cls.fi_date, wx.LIST_FORMAT_LEFT, 150),
                (cls.fi_level, wx.LIST_FORMAT_LEFT, 80),
                (cls.fi_log, wx.LIST_FORMAT_LEFT, 400)]
        
        self.list = MyListCtrlDecorator(self.lcLog, cols, cls)

    def OnBtnRefreshButton(self, event):
        self.list.refresh()

    def OnBtnExportButton(self, event):
        import os
        import kit
        
        dlg = wx.FileDialog(
            self, message=u"文件保存为", defaultDir=os.getcwd(), 
            defaultFile=u"系统日志", wildcard='*.xls', style=wx.SAVE
            )
            
        dlg.ShowModal()
        file = dlg.GetPath()
        
        if(kit.exportList(self.lcLog, file) < 0):
            msg = u'数据导出失败，请查看是不是其他程序中打开了该文件！'
        else:
            msg = u'数据完成导出'
            
        dlg = wx.MessageDialog(self, msg, u'提示',wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        
